import Vue from 'vue'
import axios from 'axios'
import _ from 'lodash'
axios.defaults.withCredentials = true;

axios.interceptors.request.use(
  config => {
    let token = sessionStorage.getItem("tokenName")
    if (token) {
      config.headers.dipsToken = `${token}`
    }
    return config;
  },
  err => {
    return Promise.reject(err)
  }
)

/* 请求超时过滤器*/
axios.interceptors.response.use(
  response => {
    const unauthorized = _.get(response, ['data', 'code'])
    if (unauthorized === 403) {
      sessionStorage.removeItem("tokenName");
      sessionStorage.removeItem("userData");
      alert('session过期，请重新登陆')
      window.location.reload();
    }
    return response;
  },
  error => {
    return Promise.reject(error.response.data)
})

Vue.prototype.$http = axios;
